<?php
namespace App\JdService;
use App\Config\config;
use App\Config\connectRedis;
use App\Config\connectMysql;
use App\Config\YlyConfig;
use App\Api\PrintService;

/**
 * Created by PhpStorm.
 * User: laodage
 * Date: 19-1-14
 * Time: 下午4:22
 */
class jdService{

    private $Redis;
    private $Db;
    private $YlyConfig;

    public function __construct()
    {
        $this->Redis = connectRedis::getInstance(config::$redis);
        $this->Db = connectMysql::getInstance(config::$mysql);
        $this->YlyConfig = new YlyConfig(config::$client[0], config::$client[1]);
    }

    public function getRedis()
    {
        return $this->Redis;
    }

    public function getDb()
    {
        return $this->Db;
    }
    public function getYlyConfig()
    {
        return $this->YlyConfig;
    }

    public function GUIDv4()
    {
        $data = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
        return vsprintf('%s%s%s%s%s%s%s%s', str_split(bin2hex($data), 4));
    }


    public function sentToPrint($machineCode, $content, $accessToken, $orderNo,$order)
    {
        $printClass = new PrintService($accessToken, $this->YlyConfig);
        $responseData = $printClass->index($machineCode, $content, $this->GUIDv4());
        $description = '打印成功';
        if (empty($responseData)) {
            $description = '打印失败订单号为：'. $orderNo . '！错误信息是：接口返回数据为null!';
        }
        if (
            isset($responseData->error) &&
            $responseData->error != 0
        ){
            $description = isset($responseData->body) ? $responseData->error_description . $responseData->body : $responseData->error_description;
        }
        $this->Redis->hset('jb_print_order_' . $orderNo . '_' . $machineCode, $description);
        if(isset($responseData->body->id)){
            //$this->pushorder($order, $responseData->body->id);
        }else {
            file_put_contents('../../Log/print_error'.date('Y-m-d').'.log',date('Y-m-d H:i:s').'下发打印失败,订单id为:'.$orderNo."\n",FILE_APPEND);
            //::report($orderNo, 'eb_print_order_error', $description);
        }
        return true;
    }

    public function getPrinter($shopId)
    {
        $sql = "SELECT a.machine_code, b.access_token, c.version FROM `jddj_print` as a, `jddj_oauth` as b, `out_print` as c WHERE a.shop_id = {$shopId} AND a.machine_code = b.machine_code AND a.machine_code = c.printid";
        return $this->Db->query($sql);
    }

    public function getShopid($token)
    {
        $sql = "SELECT shopid from `jddj_user` where token = '{$token}'";
        return $this->Db->row($sql);
    }

    public function getShopset($shopId)
    {
        $sql = 'SELECT * from `jddj_shops` where shop_id = '.$shopId;
        return $this->Db->row($sql);
    }

    //存入数据
    public function pushorder($orderData,$printid){
        $order = $orderData["order"];
        $insert = [
            //订单编号
            'order_id' => ($order["order_from"] == 1) ? $order["order_id"] : $order["eleme_order_id"],
            'client_id' => config::$client[0],
            //金额
            'money' => ($order["total_fee"])/100,
            //打印id
            'print_id' => $printid,
            //店铺id
            'shop_id' => $orderData['shop']['baidu_shop_id'],
            //点餐人
            'uname' => $orderData["user"]['name'],
            //下单时间
            'order_time' => $order["create_time"],
            //电话
            'phone' => $orderData["user"]['phone'],
            //地址
            'address' => $orderData["user"]['address'],
            //备注
            'remark' => $order["remark"],

        ];
        $this->Db->insert('new_print_data')->cols($insert)->query();
    }
}